<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="用户姓名" prop="UserName">
      <el-input v-model="ruleForm.UserName" />
    </el-form-item>
    <el-form-item label="用户密码" prop="Password">
      <el-input v-model="ruleForm.Password" />
    </el-form-item>
    <el-form-item label="用户昵称" prop="NickName">
      <el-input v-model="ruleForm.NickName" />
    </el-form-item>
    <el-form-item label="用户邮箱" prop="UserEmail">
      <el-input v-model="ruleForm.UserEmail" />
    </el-form-item>
    <el-form-item label="角色" >
      <el-select-v2
        v-model="ruleForm.RoleId"
        placeholder="Activity count"
        :options="ShowRole"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        提交
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">清空</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'

import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios'

interface RuleForm {
  UserName: string
  Password: string
  UserEmail: string
  NickName: string
  RoleId:number
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  UserName: '',
  Password: '',
  UserEmail: '',
  NickName: '',
  RoleId:0
})

const ShowRole=ref<any[]>([]);

const GetRole=()=>{
  axios({
    url:"http://localhost:5028/api/Management/GetRoleItem",
    method:"get"
  })
  .then(res=>{
    console.log(res);
    if(res.data.Code==200){
      ShowRole.value=res.data.Result
      var item=ref({
        value:0,
        label:"请选择"
      })
      ShowRole.value.push(item.value);
    }
  })
}

onMounted(()=>{
  GetRole();
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
  UserName: [
    { required: true, message: '请输入用户名称', trigger: 'blur' },
  ],
  Password: [
    { required: true, message: '请输入用户密码', trigger: 'blur' },
  ],
  UserEmail: [
    { required: true, message: '请输入用户邮箱', trigger: 'blur' },
  ],
  NickName: [
    { required: true, message: '请输入用户昵称', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      axios({
        url:"http://localhost:5117/api/Operate/InsertUser",
        method:"post",
        data:ruleForm
      })
      .then(res=>{
        console.log(res);
        if(res.data.Code==300){
          ElMessage.success("用户添加成功");
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>
